#!/bin/bash
function initGit(){
	rm -rf cur/
    mkdir cur/
    rm -rf remote/
    mkdir remote
    cat git_config | while read line
    do
		b=`echo $line | awk '{print $1}'`
		git clone git@git.oschina.net:siyi10/$b.git
		mv $b/ remote/
		if [ `echo $line | awk '{print $2}'` -eq 1 ]
		then
			a=`echo $line | awk '{print $1}'`
			branch=`echo $line | awk '{print $3}'`
			git clone -b $branch git@192.168.1.7:backendsource/$a.git
			mv $a/ cur/
		else
			a=`echo $line | awk '{print $1}'`
			if [ $a == 'search-api' ]
			then
				branch=`echo $line | awk '{print $3}'`
				git clone -b $branch git@192.168.1.7:zhen-api/search.git
				mv search/ cur/
			else
				branch=`echo $line | awk '{print $3}'`
				git clone -b $branch git@192.168.1.7:zhen-api/$a.git
				mv $a/ cur/
			fi
		fi
    done
}
function mvDir(){
	cat git_config | awk '{print $1}' | while read line
	do
		if [ `echo $line` == 'search-api' ]
		then
			ls cur/search/ | grep -v ".git*" | xargs -t -i cp -rf cur/search/{} remote/search/
		else
			ls cur/$line/ | grep -v ".git*" | xargs -t -i cp -rf cur/$line/{} remote/$line/
		fi
		
	done
}

function commit(){
	cat git_config | awk '{print $1}' | while read line
	do
		cd remote/$line
		git add .
		git commit -m '自动提交'
		git push
		cd ..
	done
}
function clean(){
	rm -rf remote/
	rm -rf cur/
}

initGit
mvDir
commit
clean
